package models

import (
	"time"
	"xorm.io/builder"
)

type LearnRecordGnss struct {
	Id            int `xorm:"not null pk autoincr comment('自增主键') INT(11)"`
	LearnRecordId int `xorm:"not null comment('学车学时id外键关联dts_learn_record表中的id') index INT(11)"`
	GnssId        int `xorm:"not null comment('gnss位置信息id外键关联gnss表中的id') index INT(11)"`
}

type RercodGnss struct {
	Id                 int64
	LearnRecordId      int
	Learnrecord_number string
	Type               string
	Approval_status    string
	GnssId             int
	Subject            string
	Add_time           time.Time
	Image              string
	Maxspeed           float32
	Velocitycar        float32
	Enginespeed        int
	Checkfence         string
	Mileage            float32
	Reason             string
	Latitude           float32
	Longitude          float32
	Datetime           time.Time
}

func (c *LearnRecordGnss) Zero(id string) (count int64, err error) {
	count, err = engine.Table("dts_learn_record_gnss").Alias("ls").
		Join("left", []string{"dts_gnss", "gs"}, "ls.gnss_id= gs.id").
		Where("ls.learn_record_id = ? and gs.velocitycar = ? and gs.learnrecord_number > ?", id, "0", "0").Count()
	return count, err
}

func (c *LearnRecordGnss) ZeroFive(id string) (count int64, err error) {
	count, err = engine.Table("dts_learn_record_gnss").Alias("ls").
		Join("left", []string{"dts_gnss", "gs"}, "ls.gnss_id= gs.id").
		Where("ls.learn_record_id = ? and gs.velocitycar > ? and gs.velocitycar <= ? and gs.learnrecord_number > ?", id, "0", "50", "0").Count()
	return count, err
}

func (c *LearnRecordGnss) Five(id string) (count int64, err error) {
	count, err = engine.Table("dts_learn_record_gnss").Alias("ls").
		Join("left", []string{"dts_gnss", "gs"}, "ls.gnss_id= gs.id").
		Where("ls.learn_record_id = ? and gs.velocitycar > ? and gs.learnrecord_number > ?", id, "50", "0").Count()
	return count, err
}

func (c *LearnRecordGnss) Renke(id string) (count int64, err error) {
	count, err = engine.Table("dts_learn_record_gnss").Alias("ls").
		Join("left", []string{"dts_gnss", "gs"}, "ls.gnss_id= gs.id").
		Where("ls.learn_record_id = ? and gs.approval_status = ? and gs.learnrecord_number > ?", id, "1", "0").Count()
	return count, err
}

func (c *LearnRecordGnss) GetGnss(id string) (list []*RercodGnss, err error) {
	err = engine.Table("dts_learn_record_gnss").Alias("ls").
		Select("ls.id,ls.learn_record_id,ls.gnss_id,gs.add_time,gs.Maxspeed,gs.checkfence,gs.enginespeed,gs.latitude,gs.longitude,gs.datetime,gs.reason,gs.learnrecord_number,gs.subject,gs.type,gs.approval_status,gs.mileage,gs.velocitycar").
		Join("left", []string{"dts_gnss", "gs"}, "ls.gnss_id= gs.id").
		Where("ls.learn_record_id = ?  and gs.learnrecord_number > ?", id, "0").
		Find(&list)
	return list, err
}

type GnssInfo struct {
	Latitude  float64
	Longitude float64
	Id        int64
	Datetime  string
}

func (c *LearnRecordGnss) GetGnssByTime(id int64, filter builder.Cond) (list []*GnssInfo, err error) {
	err = engine.Table("dts_learn_record_gnss").Alias("ls").
		Select("gs.latitude,gs.longitude,gs.datetime,gs.id").
		Join("left", []string{"dts_gnss", "gs"}, "ls.gnss_id= gs.id").
		And(filter).
		Where("ls.learn_record_id =?", id).
		OrderBy("gs.datetime").
		Find(&list)
	return list, err
}
